// 时间大小比较 
function CompareDate(d1, d2) {
    return ((new Date(d1.replace(/-/g, "\/"))) <= (new Date(d2.replace(/-/g, "\/"))));
}
//判断时间在不在区间里面
var date = {
    isDuringDate: function (beginDateStr, endDateStr) {
        var curDate = new Date(),
            beginDate = new Date(beginDateStr),
            endDate = new Date(endDateStr);
        if (curDate >= beginDate && curDate <= endDate) {
            return "进行中";
        }
        return "未开始";
    }
}

// 获取当前时间
function currentDate() {
    var myDate = new Date();
    let year = myDate.getFullYear();
    let mounth = myDate.getMonth() + 1;
    let day = myDate.getDate();
    mounth = JSON.stringify(mounth)
    day = JSON.stringify(day)
    if (mounth.length < 2) {
        mounth = "0" + mounth
    }
    if (day.length < 2) {
        day = "0" + day
    }
    let currentTime = JSON.stringify(year) + '-' + mounth + "-" + day
    return currentTime
}
// 获取七天后的时间
function sevenData() {
    var now = new Date();
    var date = new Date(now.getTime() + 7 * 24 * 3600 * 1000);
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();
    month = JSON.stringify(month)
    day = JSON.stringify(day)
    if (month.length < 2) {
        month = "0" + month
    }
    if (day.length < 2) {
        day = "0" + day
    }
    return year + '-' + month + '-' + day;
}

// 大数相加
function sumBigNumber(a, b) {
    var res = '',
        temp = 0;
    a = a.split('');
    b = b.split('');
    while (a.length || b.length || temp) {
        temp += ~~a.pop() + ~~b.pop();
        res = (temp % 10) + res;
        temp = temp > 9;
    }
    return res.replace(/^0+/, '');
}

// 验证url地址
function isUrl(val) {
    var rex = /^(http|https){1}:\/\/[^\s]+$/;
    if (rex.test(val)) {
        // console.log("是url地址")
        return true
    } else {
        return false
        // console.log("不是url地址")
    }
}
// 验证邮箱地址
function email(val) {
    var reg = /^\w+@[a-zA-Z0-9]{2,10}(?:\.[a-z]{2,4}){1,3}$/;
    if (!reg.test(val))
        return false
}
// 验证身份证
function identityId(val) {
    let regIdCard = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/

    // 如果通过该验证，说明身份证格式正确，但准确性还需计算
    if (regIdCard.test(val)) {
        if (val.length == 18) {
            let idCardWi = new Array(
                7,
                9,
                10,
                5,
                8,
                4,
                2,
                1,
                6,
                3,
                7,
                9,
                10,
                5,
                8,
                4,
                2
            ) // 将前17位加权因子保存在数组里
            let idCardY = new Array(1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2) // 这是除以11后，可能产生的11位余数、验证码，也保存成数组
            let idCardWiSum = 0 // 用来保存前17位各自乖以加权因子后的总和
            for (let i = 0; i < 17; i++) {
                idCardWiSum += val.substring(i, i + 1) * idCardWi[i]
            }
            let idCardMod = idCardWiSum % 11 // 计算出校验码所在数组的位置
            let idCardLast = val.substring(17) // 得到最后一位身份证号码
            // 如果等于2，则说明校验码是10，身份证号码最后一位应该是X
            if (idCardMod == 2) {
                if (idCardLast == 'X' || idCardLast == 'x') {
                    return true
                } else {
                    return false
                }
            } else {
                // 用计算出的验证码与最后一位身份证号码匹配，如果一致，说明通过，否则是无效的身份证号码
                if (idCardLast == idCardY[idCardMod]) {
                    return true
                } else {
                    return false
                }
            }
        }
    } else {
        return false
    }
}


export {
    CompareDate,
    date,
    sumBigNumber,
    isUrl,
    email,
    identityId,
    currentDate,
    sevenData
}